package com.sicnu.cheer.leaugebar.contacts.activity;

import android.app.ProgressDialog;
import android.content.Intent;
import android.net.Uri;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.sicnu.cheer.generalmodule.activity.BaseActivity;
import com.sicnu.cheer.generalmodule.bean.JoinLeagueBean;
import com.sicnu.cheer.generalmodule.bean.MyUser;
import com.sicnu.cheer.generalmodule.util.StringUtils;
import com.sicnu.cheer.generalmodule.util.UIHelper;
import com.sicnu.cheer.leaugebar.contacts.R;

import cn.bmob.v3.listener.UpdateListener;
import io.rong.imkit.RongIM;

/**
 * 用户详细信息展示页
 */
public class UserInfoActivity extends BaseActivity implements View.OnClickListener {
    private ImageView userHeadIV;
    private TextView userNameTV;
    private ImageView sexIV;
    private TextView sexTV;
    private TextView hometownTV;
    private TextView gradeTV;
    private TextView majorTV;
    private TextView phoneTV;
    private TextView qqTV;
    private ImageView callIV;
    private JoinLeagueBean joinLeague;
    private MyUser user;
    private boolean isAdd;
    private Button sendMessage;

    private ProgressDialog progressDialog;
    @Override
    public int getContentViewId() {
        return R.layout.activity_user_info;
    }

    @Override
    protected void initView() {
        super.initView();
        findViewById(R.id.back).setOnClickListener(this);
        userHeadIV = ((ImageView) findViewById(R.id.detail_userhead_iv));
        userNameTV = ((TextView) findViewById(R.id.detail_username_tv));
        sexIV = ((ImageView) findViewById(R.id.sex_iv));
        sexTV = ((TextView) findViewById(R.id.sex_tv));
        hometownTV = ((TextView) findViewById(R.id.hometown_tv));
        gradeTV = ((TextView) findViewById(R.id.grade_tv));
        majorTV = ((TextView) findViewById(R.id.major_tv));
        callIV = ((ImageView) findViewById(R.id.call_iv));
        callIV.setOnClickListener(this);
        phoneTV = ((TextView) findViewById(R.id.phone_tv));
        qqTV = ((TextView) findViewById(R.id.qq_tv));
        sendMessage = ((Button) findViewById(R.id.send_message_btn));
        sendMessage.setOnClickListener(this);
    }

    @Override
    protected void initData() {
        super.initData();
        Intent intent = getIntent();
        joinLeague= (JoinLeagueBean) intent.getSerializableExtra("joinLeague");
        if (joinLeague!=null){
            user=joinLeague.getMyUser();
        }else {
            user = (MyUser) intent.getSerializableExtra("user");
        }
        isAdd = intent.getBooleanExtra("isAdd",false);

        progressDialog = new ProgressDialog(mThis);
        progressDialog.setMessage("加载中...");
    }

    @Override
    protected void afterViews() {
        super.afterViews();
        if (user !=null) {
            Glide.with(mThis).load(user.getHeadPic()).placeholder(R.mipmap.default_user_circle).into(userHeadIV);
            userNameTV.setText(user.getNick());
            sexIV.setImageResource(user.getSex().equals("女")?R.mipmap.female:R.mipmap.male);
            sexTV.setText(user.getSex());
            if (!StringUtils.isEmpty(user.getHometown())) {
                hometownTV.setText(user.getHometown().split("-")[1]);
            }
            gradeTV.setText(user.getGrade());
            majorTV.setText(user.getMajor());
            phoneTV.setText(user.getMobilePhoneNumber());
            qqTV.setText(user.getQq());
            sendMessage.setText(isAdd?"确定添加":"发送消息");
        }
    }

    @Override
    public void onClick(View v) {
        if (v.getId()==R.id.back){
            onBackPressed();
        }else if (v.getId()==R.id.call_iv){
            Intent intent = new Intent(Intent.ACTION_DIAL);
            intent.setData(Uri.parse("tel:" + String.valueOf(user.getMobilePhoneNumber())));
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(intent);
        }else if (v.getId()==R.id.send_message_btn){
            if (isAdd){
                joinLeague.setManager(true);
                progressDialog.show();
                joinLeague.update(mThis, joinLeague.getObjectId(), new UpdateListener() {
                    @Override
                    public void onSuccess() {
                        progressDialog.dismiss();
                        UIHelper.ToastMessage(mThis,"添加管理员成功");
                        setResult(1);
                        finish();
                    }

                    @Override
                    public void onFailure(int i, String s) {
                        progressDialog.dismiss();
                        UIHelper.ToastMessage(mThis,"添加管理员失败:"+s);
                    }
                });
            }else {
                RongIM.getInstance().startPrivateChat(this, user.getObjectId(), user.getNick());
            }
        }
    }
}
